//package org.example;
//
//import org.apache.commons.codec.binary.Base64;
//import org.apache.http.HttpResponse;
//import org.apache.http.HttpStatus;
//import org.apache.http.client.HttpClient;
//import org.apache.http.client.methods.HttpPost;
//import org.apache.http.entity.StringEntity;
//import org.apache.http.impl.client.HttpClients;
//import org.apache.http.util.EntityUtils;
//
//import javax.crypto.Mac;
//import javax.crypto.spec.SecretKeySpec;
//import java.net.URLEncoder;
//
//public class DingTalkRobot {
//
//	/**
//	 * 钉钉机器人签名
//	 */
//	private String secret;
//
//	/**
//	 * 钉钉机器人webhook
//	 */
//	private String url;
//
//
//	public DingTalkRobot(String secret, String url) {
//		this.secret = secret;
//		this.url = url;
//	}
//
//	/*
//	 * 方法描述 获取签名
//	 */
//	private String signature(String timestamp) throws Exception {
//		String stringToSign = timestamp + "\n" + secret;
//		Mac mac = Mac.getInstance("HmacSHA256");
//		mac.init(new SecretKeySpec(secret.getBytes("UTF-8"), "HmacSHA256"));
//		byte[] signData = mac.doFinal(stringToSign.getBytes("UTF-8"));
//		return URLEncoder.encode(new String(Base64.encodeBase64(signData)), "UTF-8");
//	}
//
//
//	public String sendLinkMsg(String msg) throws Exception {
//		String timestamp = String.valueOf(System.currentTimeMillis());
//		HttpClient httpclient = HttpClients.createDefault();
//		String postUrl = url + "&timestamp=" + timestamp + "&sign=" + signature(timestamp);
//		HttpPost httppost = new HttpPost(postUrl);
//		httppost.addHeader("Content-Type", "application/json; charset=utf-8");
//		httppost.addHeader("sign", signature(timestamp));
//		httppost.addHeader("timestamp", timestamp);
//		StringEntity se = new StringEntity(msg, "utf-8");
//		httppost.setEntity(se);
//		String result = null;
//		HttpResponse response = httpclient.execute(httppost);
//		if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
//			result = EntityUtils.toString(response.getEntity());
//		}
//		return result;
//	}
//
//}
//
